package com.yc.favorite.servlet;

import com.yc.favorite.biz.FavoriteBiz;
import com.yc.favorite.entity.Favorite;
import com.yc.favorite.entity.User;
import com.yc.favorite.util.ServletUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addFav")
public class AddFavServlet extends HttpServlet {

    private FavoriteBiz fbiz = new FavoriteBiz();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("myinfo");
        Favorite favorite = new Favorite();
        favorite.setFlabel(req.getParameter("flabel"));
        favorite.setFurl(req.getParameter("furl"));
        favorite.setFtags(req.getParameter("ftags"));
        favorite.setFdesc(req.getParameter("fdesc"));
        if(user!=null){
            favorite.setUid(user.getId());
        }
        ServletUtils.result("新增链接成功!", resp,
            ()->{
                fbiz.createFavorite(favorite);
            });
    }
}
